﻿using System;
using System.Data;
using System.Web.UI.WebControls;
using VipCo.Data;
using VipCo.Entities;

public partial class Kq_Timkiem : System.Web.UI.Page
{
    // Khai bao cac bien toan cuc
    private static string _tuKhoa = null;
    private static string _id = null;
    private static string _idCm = null;
    private static string _tenCm = null;
    private static string _idQuanhuyen = null;
    private static int _idTpSs = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            // Thuc hien lay cac params
            _tuKhoa = Request.Params["Tukhoa"];
            _id = Request.Params["Id"];
            _idCm = Request.Params["IdCm"];
            _tenCm = Request.Params["TenChuyenMuc"];
            _idQuanhuyen = Request.Params["IdQh"];
            _idTpSs = Convert.ToInt32(Session["IdTp"]);

            if (_tuKhoa != null)
            {
                // Thuc hien tim kiem va hien thi du lieu theo tu khoa
                HienthiTinKqTimkiem(Pager1.CurrentIndex, _tuKhoa);
                litTukhoa.Text = _tuKhoa;
            }
            else
            {
                if (_idQuanhuyen != null)
                {
                    // Thuc hien tim kiem du lieu theo Quan Huyen
                    HienthiTinKqTimkiemTheoQuanhuyen(Pager1.CurrentIndex, Convert.ToInt32(_idQuanhuyen));
                }
                else
                {
                    // Tim kiem theo chuyen muc
                    HienthiTinKqTimkiemTheoChuyenMuc(Pager1.CurrentIndex, Convert.ToInt32(_idCm));
                    // Lay ten chuyen muc
                    NhomChuyenMuc entChuyenmuc = DataRepository.NhomChuyenMucProvider.GetById(Convert.ToInt32(_idCm));
                    litTukhoa.Text = entChuyenmuc.TenChuyenMuc.ToString();
                    lbSubtin.Text = entChuyenmuc.TenChuyenMuc.ToString();
                    lbSubtin.Visible = true;
                }
            }
        }

    }

    /// <summary>
    /// Hien thi tin tuc khuyen mai
    /// </summary>
    /// <param name="pageNo"></param>
    /// <param name="tukhoa"></param>
    protected void HienthiTinKqTimkiem(int pageNo, string tukhoa)
    {
        int totalRows = 0;
        string whereClause = "dbo.fuChuyenCoDauThanhKhongDau(TenDiaDiem) like N'%" + Common.StandardString(tukhoa) + "%' and IdThanhPho = " + _idTpSs;
        DataSet dsDiemDen = DataRepository.Provider.ExecuteDataSet("DiaDiem_GetPaged", whereClause, "Id DESC", pageNo - 1, Pager1.PageSize);

        dlTimkiem.DataSource = dsDiemDen.Tables[0];
        dlTimkiem.DataBind();

        try
        {
            totalRows = Convert.ToInt32(dsDiemDen.Tables[1].Rows[0][0]);
        }
        catch (Exception)
        {
        }
        Pager1.ItemCount = totalRows;
        if (totalRows <= Pager1.PageSize)
            Pager1.Visible = false;
        else
            Pager1.Visible = true;
    }

    protected void HienthiTinKqTimkiemTheoChuyenMuc(int pageNo, int idCmP)
    {
        int totalRows = 0;
        string whereClause = "IdChuyenMuc = " + idCmP + " and IdThanhPho = " + _idTpSs;
        DataSet dsDiemDen = DataRepository.Provider.ExecuteDataSet("DiaDiem_GetPaged", whereClause, "Id DESC", pageNo - 1, Pager1.PageSize);

        dlTimkiem.DataSource = dsDiemDen.Tables[0];
        dlTimkiem.DataBind();

        try
        {
            totalRows = Convert.ToInt32(dsDiemDen.Tables[1].Rows[0][0]);
        }
        catch (Exception)
        {
        }
        Pager1.ItemCount = totalRows;
        if (totalRows <= Pager1.PageSize)
            Pager1.Visible = false;
        else
            Pager1.Visible = true;
    }

    protected void HienthiTinKqTimkiemTheoQuanhuyen(int pageNo, int idQhP)
    {
        int totalRows = 0;
        string whereClause = "IdQuanHuyen = " + idQhP + " and IdThanhPho = " + _idTpSs;
        DataSet dsDiemDen = DataRepository.Provider.ExecuteDataSet("DiaDiem_GetPaged", whereClause, "Id DESC", pageNo - 1, Pager1.PageSize);

        dlTimkiem.DataSource = dsDiemDen.Tables[0];
        dlTimkiem.DataBind();

        try
        {
            totalRows = Convert.ToInt32(dsDiemDen.Tables[1].Rows[0][0]);
        }
        catch (Exception)
        {
        }
        Pager1.ItemCount = totalRows;
        if (totalRows <= Pager1.PageSize)
            Pager1.Visible = false;
        else
            Pager1.Visible = true;
    }

    public string GetTieuDe(object Char)
    {
        string s = Char.ToString();
        return Common.ConvertToUnSign(s);
    }

    public string FormatString(string str, int n)
    {
        string kq = "";

        if (str.Length > n)
            kq = str.Substring(0, n + 1) + "...";
        else
            kq = str;

        return kq;
    }

    protected void Pager1_Command(object sender, CommandEventArgs e)
    {
        int currnetPageIndex = Convert.ToInt32(e.CommandArgument);
        Pager1.CurrentIndex = currnetPageIndex;

        // Kiem tra dieu kien xem la phan trang theo cai nao
        if (_tuKhoa != null)
        {
            // Thuc hien tim kiem va hien thi du lieu theo tu khoa
            HienthiTinKqTimkiem(currnetPageIndex, _tuKhoa);
        }
        else
        {
            if (_idQuanhuyen != null)
            {
                // Thuc hien tim kiem du lieu theo Quan Huyen
                HienthiTinKqTimkiemTheoQuanhuyen(currnetPageIndex, Convert.ToInt32(_idQuanhuyen));
            }
            else
            {
                // Thuc hien tim kiem theo ca 3 dieu kien
                HienthiTinKqTimkiemTheoChuyenMuc(currnetPageIndex, Convert.ToInt32(_idCm));
            }
        }
    }
}